var parkingSpaceQueryJs = function (targetObj) {
	this.targetObj = targetObj;
	this.sysUtils = new kendoUtils();
	this.init();
	return this;
};

parkingSpaceQueryJs.prototype = {
		init : function () {
			var fieldArray = this.sysUtils.getBindFieldArray(this.targetObj.find('.search-panel'));
			var functionArray = [
			                     {fName : 'search_btn' , fs : this.findData},
			                     {fName : 'reset_btn' , fs : this.resetInfo}
			                     ];
			this.models = new bindBaseModel(null, fieldArray, functionArray, null);
			this.sysUtils.bindDoucment(this.models, this.targetObj, this);
			this.initHtml();
		},
		/**
		 * 初始化界面元素
		 */
		initHtml:function(){
			var _this = this;
			var columns = [ {
				width : 30,
				headerTemplate : '<input type="checkbox" gridId="parkingSpaceQueryGrid" onclick="gridCheckAll(this)" />',
				template : '<input type="checkbox" name="spaceQueryId" value="${spaceCode}" />',
			},
			{
				title : '车位编号',
				field : 'spaceCode'
			},
			{
				title : '车位类型',
				template : '#:Utils.getModelNameByParentKey(BASIS.VEHICLE_TYPE.ROOT, data.spaceType)#'
			},
			{
				title : '到期时间',
				field : 'endTime'
			},
			{
				title : '是否占用',
				template : '#if(0==data.spaceStatus){#空闲#}else{#已占用#}#'
			},
			{
				title : '车主名称',
				template : '#:data.ownerName#(#:data.ownerCode#)'
			},
			{
				title : '联系电话',
				field : 'ownerPhone'
			},
			{
				title : '入场凭证',
				template : '#if(undefined!=data.vehicleNo){##:vehicleNo# / #} if(undefined != data.uniqueIdentifier){##:uniqueIdentifier# #}#'
			},
			{
				title : '入场时间',
				field : 'inTime'
			},
			];
			//计算显示区域高度
			var heights = this.targetObj.height() - this.targetObj.find(".search-panel").outerHeight();
			this.sysUtils.createGird(this.targetObj.find('#parkingSpaceQueryGrid'), {
				selectable : 'single',
				autoSync : true,
				columnMenu : true,
				height:heights,
				pageable : {input : true, numeric : false},
				columns : columns,
				dataBound:function(){
					var tableList = _this.targetObj.find("#parkingSpaceQueryGrid");
					tableList.find("tr:gt(0)").unbind("dblclick");
					tableList.find("tr:gt(0)").dblclick(function(e){
						var grid = tableList.data("kendoGrid");
						var dataItem = grid.dataItem(grid.select());
						if(!isEmpty(dataItem.inoutRecordId)){   //车位被占用，才能弹出进出记录显示框
							//打开查看窗口
							new JsonpAjax(baseApiPath+"recordMarket/" + dataItem.inoutRecordId,null,"json",function(result){
								if(isEmpty(result) || !result.hasOwnProperty("entity")){
									notification({title : '提示', message : '查无此车位的占用进出详情!!'}, 'info');
									return;
								}
								window.showInoutRecodeView = result;
								psQueryJs.editDiv = psQueryJs.sysUtils.openWindow(psQueryJs.editDiv,{
									title:"查看进出详情",
									height:"680px",
									width:"940px",
									resizable:false,
									content:"views/parkLotCenter/recordMarket/inoutRecoderEdit.html"
								}, true);
							});
						}else{
							notification({title : '提示', message : '车位空闲时不能查看占用详情!!'}, 'info');
						}
					});	
				}
			});
		},
		/*
		 * 重置按钮
		 */
		resetInfo:function(e){
			var that = $(e.target).data("that");
			that.sysUtils.setBindFieldNull(that.models.bindFieldArray);
		},
		/**
		 * 查询列表数据
		 * @param e
		 */
		findData : function (e) {
			var _this = this;
			if (!isEmpty(e)) {
				_this = $(e.target).data('that');
			}
			if (validityForm(_this.targetObj)) {
				var grid = _this.targetObj.find('#parkingSpaceQueryGrid').data('kendoGrid');
				var params = _this.sysUtils.getBindDocJson(_this.models.bindFieldArray);
				var dataT = {'jsonParam' : JSON.stringify(params)};
				var dataSource = _this.sysUtils.getDataSource({url : baseApiPath + 'parking_space/spacelist', data : dataT, dataType : 'odata'});
				grid.setDataSource(dataSource);
			}
		},
		/**
		 * 把占用的车位通过人工操作释放出来
		 */
		releaseSpace:function(){
			var checkboxs = this.targetObj.find("input[name='spaceQueryId']:checked");
			if(checkboxs.length <= 0){
				notification({title : '提示',message : '请至少选择一个车位做释放车位操作！！'});
				return;
			}
			var that = this;
			var spaceCode = "";
			checkboxs.each(function(){
				spaceCode +="'" + $(this).val() + "'" + SEPARATOR;
			});
			spaceCode = spaceCode.substring(0, spaceCode.length - 1);
			notification({title : '提示', message : '是否确认把勾选车位状态改为空闲？确定后此操作无法回退撤销！！'}, 'confirm', {
				callback : function () {
					JsonpAjax(baseApiPath + 'parking_space/updateSpaceStatus', spaceCode, 'json', function ( res ) {
						that.findData();
					}, true);
				}
			})
		}
}